package ac.svn.accesscontrol.store.validator;

import ac.svn.accesscontrol.context.Context;
import ac.svn.accesscontrol.context.IContextConstants;
import ac.svn.accesscontrol.store.SVNUser;

public class UserOperationValidator implements IProperty,IContextConstants
{

    /***
     * Default constructor of class.
     */
    public UserOperationValidator()
    {

    }

    public ValidationResult validateObject(Context context)
    {
	ValidationResult validationResult = new ValidationResult();

	//Check whether context is valid or not.
	if(null != context)
	{
	    //Get user object from context save against KEY_OPERATION_PARAM.
	    SVNUser user = (SVNUser) context.get(KEY_OPERATION_PARAM);

	    //Check if user information is present.
	    if(null != user)
	    {
		String userName = user.getUserName();
		String userId   = user.getId();

		if((null == userName) || (true == userName.isEmpty()))
		{
		    validationResult.setValid(false);
		    validationResult.getErrors().put(ERROR_USER_NAME,"User details missing \"User Name\", Kindly mention a unique name!");
		}

		if((null == userId) || (true == userId.isEmpty()))
		{
		    validationResult.setValid(false);
		    validationResult.getErrors().put(ERROR_USER_ID,"User details missing \"User Id\", Kindly mention a unique Id!");
		}
	    }
	    else
	    {
		validationResult.setValid(false);
		validationResult.getErrors().put(ERROR_USER,"Context doesn't contain user details, Are we missing any step?");
	    }

	}
	else
	{
	    validationResult.setValid(false);
	    validationResult.getErrors().put(ERROR_CONTEXT,"Context is not present for operation!");
	}
	return validationResult;
    }

    public String formatMessage(String title,ValidationResult validationResult)
    {
	String message = String.format("<HTML><b> %s, details</b>:<br>",title);
	if(false == validationResult.getErrors().isEmpty())
	{
	    int count = 1;
	    for(String key: validationResult.getErrors().keySet())
	    {
		String subMsg = (String) validationResult.getErrors().get(key); 
		message = String.format("%s<br>%d. [Err] %s",message,count++,subMsg);
	    }
	}
	if(false == validationResult.getWarningsDetails().isEmpty())
	{
	    int count = 1;
	    for(String key: validationResult.getWarningsDetails().keySet())
	    {
		String subMsg = (String) validationResult.getWarningsDetails().get(key); 
		message = String.format("%s<br>%d. [War] %s",message,count++,subMsg);
	    }
	}
	return message;
    }

    public boolean checkModifications(SVNUser orgDetails,SVNUser newDetails)
    {
	boolean compResult  = false;

	//Check if both arguments are present.
	if((null != orgDetails)&&(null != newDetails))
	{
	    //Check Name and Id fields.
	    if(orgDetails.getUserName().equalsIgnoreCase(newDetails.getUserName()))
	    {
		if(false == orgDetails.getUserAlias().equalsIgnoreCase(newDetails.getUserAlias()))
		{
		    compResult = true;
		}
	    }
	    else
	    {
		compResult = true;
	    }

	    //If above is comparison produced negative result.
	    if(false == compResult)
	    {
		//Check if group membership has changed.
		if(orgDetails.getGroups().size() == newDetails.getGroups().size())
		{
		    if(false == (orgDetails.getGroups().containsAll(newDetails.getGroups())
			    &&
			    newDetails.getGroups().containsAll(orgDetails.getGroups())))
		    {
			compResult = true;
		    }
		}	
	    }
	}
	else if(null != newDetails)
	{
	    compResult = true;
	}
	
	

	return compResult;
    }

}
